import 'package:flutter/material.dart';

class AnimationTestPage extends StatefulWidget {
  static String routeName = "/AnimationTestPage";

  const AnimationTestPage({Key? key}) : super(key: key);

  @override
  _AnimationTestPageState createState() => _AnimationTestPageState();
}

class _AnimationTestPageState extends State<AnimationTestPage>
    with TickerProviderStateMixin {
  List<Widget> anims = [];
  Offset offset = Offset.zero;

  late AnimationController _rotateController;

  @override
  void initState() {
    super.initState();
    // 初始化AnimationController
    _rotateController = AnimationController(
      duration: const Duration(seconds: 5), // 设置旋转一圈的持续时间
      vsync: this,
    )..repeat(); // 无限重复模式
  }


  @override
  void dispose() {
    super.dispose();
    _rotateController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("动画测试")),
      body: GestureDetector(
        onTapDown: (details) {
          offset = details.localPosition;
        },
        onTap: () {
          addAnimWidget();
        },
        child: Container(
          color: const Color.fromARGB(50, 250, 20, 190),

          child: Stack(
            children: [
              Align(
                alignment: Alignment.center,
                child: RotationTransition(
                  turns: _rotateController,
                  child: Container(
                    width: 50,
                    height: 50,
                    color: Colors.red,
                  ),
                ),
              )
              ,Stack(
              children: anims,
            )],
          ),
        ),
      ),
    );
  }

  addAnimWidget() async {
    final AnimationController controller = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    );

    final Animation<double> curve =
        CurvedAnimation(parent: controller, curve: Curves.easeInOut);

    ///不透明度
    final Animation<double> opacityAnimation =
        Tween<double>(begin: 1.0, end: 0.0).animate(curve);

    ///尺寸大小
    final Animation<double> sizeAnimation =
        Tween<double>(begin: 1.0, end: 2.0).animate(curve);

    ///位移
    final Animation<Offset> translateAnimation =
        Tween<Offset>(begin: Offset.zero, end: const Offset(30, -30))
            .animate(curve);

    ///旋转（弧度单位）
    final Animation<double> rotateAnimation =
        Tween(begin: 0.0, end: 0.2).animate(curve);

    Widget widget = Positioned(
        left: offset.dx - 15,
        top: offset.dy - 15,
        child: AnimatedBuilder(
            animation: controller,
            builder: (context, widget) {
              return Opacity(
                opacity: opacityAnimation.value,
                child: Transform.scale(
                  scale: sizeAnimation.value,
                  child: Transform.translate(
                    offset: translateAnimation.value,
                    child: Transform.rotate(
                      angle: rotateAnimation.value,
                      child: Container(
                        decoration: const BoxDecoration(
                            image: DecorationImage(
                                image: AssetImage("assets/image/xin.png"))),
                        width: 30,
                        height: 30,
                      ),
                    ),
                  ),
                ),
              );
            }));
    setState(() {
      anims.add(widget);
    });
    await controller.forward();
    setState(() {
      anims.remove(widget);
    });
  }
}

class AnimationItem {
  AnimationController controller;
  Animation<double> opacityAnimation;
  Animation<double> sizeAnimation;
  Offset position; // 点击位置

  AnimationItem({
    required this.controller,
    required this.opacityAnimation,
    required this.sizeAnimation,
    required this.position,
  });
}
